package com.yaosheng.thread.interview;

import java.util.concurrent.Exchanger;

/**
 * Created by yaosheng on 2021/7/6.
 */
public class ExchangerView {

    private static Exchanger<String> exchanger = new Exchanger<> ();

    public static void main(String[] args) {

        char[] aI = "1234567".toCharArray ();
        char[] aC = "ABCDEFG".toCharArray ();

        new Thread ( () -> {
            for(int i = 0;i < aI.length;i ++){
                System.out.println (aI[i]);
                try{
                    exchanger.exchange ("T1");
                } catch (InterruptedException e) {
                    e.printStackTrace ();
                }
            }
        }).start ();

        new Thread ( () -> {
            for(int i = 0;i < aC.length;i ++){
                System.out.println (aC[i]);
                try{
                    exchanger.exchange ("T2");
                } catch (InterruptedException e) {
                    e.printStackTrace ();
                }
            }
        }).start ();
    }
}
